package com.lee.dianjiqi.utils;

import android.util.Log;

import java.util.Iterator;
import java.util.LinkedHashMap;
import java.util.Map;
import java.util.Set;

public class MyNotice {

    private static final String TAG = "MyNotice";
    private Map<String, MyListener> maps = new LinkedHashMap<>();

    private static MyNotice instance;

    public static MyNotice getInstance() {
        if (instance == null) {
            synchronized (MyNotice.class) {
                if (instance == null) {
                    instance = new MyNotice();
                }
            }
        }
        return instance;
    }

    private MyNotice() {
    }

    public void registerListener(String key, MyListener myListener) {
        if (maps == null)
            maps = new LinkedHashMap<>();
        maps.put(key, myListener);
    }

    public void notifyMy(String key) {

        if (maps == null) {
            Log.d(TAG, "=======map集合为空=========");
            return;
        }

        MyListener myListener = maps.get(key);

        if (myListener != null)
            myListener.Listener();

    }

    public interface MyListener {
        void Listener();
    }


    public void clear() {

        if (maps != null) {
            Set<String> strings = maps.keySet();
            Iterator<String> iterator = strings.iterator();
            while (iterator.hasNext()) {
                String next = iterator.next();
                MyListener myListener = maps.get(next);
                myListener = null;
            }
            maps.clear();
        }
        maps = null;
    }
}